package com.nineclock.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.document.mapper.DocFileHistoryMapper;
import com.nineclock.document.pojo.DocFileHistory;
import com.nineclock.document.service.DocFileHistoryService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysCompanyUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DocFileHistoryServiceImpl implements DocFileHistoryService {

    @Autowired
    private DocFileHistoryMapper docFileHistoryMapper;

    @Autowired
    private SysCompanyUserFeign companyUserFeign;


    @Override
    public List<DocFileHistoryDto> queryFileHistory(Long fileId) {
        //参数校验
        if (fileId == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        LambdaQueryWrapper<DocFileHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocFileHistory::getFileId,fileId);
        List<DocFileHistory> fileHistoryList = docFileHistoryMapper.selectList(wrapper);

        List<DocFileHistoryDto> fileHistoryDtoList = BeanHelper.copyWithCollection(fileHistoryList, DocFileHistoryDto.class);

        //遍历
        for (DocFileHistoryDto docFileHistoryDto : fileHistoryDtoList) {
            SysCompanyUserDTO sysCompanyUserDTO = companyUserFeign.queryCompanyUserById(docFileHistoryDto.getOperateUserId()).getData();
            //设置用户名
            docFileHistoryDto.setUserName(sysCompanyUserDTO.getUserName());
        }
        //转换类型并返回
        return fileHistoryDtoList;
    }
}
